package com.jing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jing.mapper.UserMapper;
import com.jing.pojo.PageBean;

import com.jing.pojo.User;
import com.jing.service.UserService;
import com.jing.utils.Md5Util;
import com.jing.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username, String role) {
        User u = userMapper.findByUserName(username, role);
        return u;
    }

    @Override
    public void register(String username, String password, String nickname, String email, String phoneNumber) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        User user = new User();
        String imgUrl="1";
        //添加
        userMapper.add(username, md5String, nickname, email, phoneNumber,imgUrl);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }
//
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }




    @Override
    public int findRole(Integer id, String username) {
        return userMapper.findRole(id, username);

    }

    @Override
    public PageBean<User> getUserList(Integer pageNum, Integer pageSize, String username, String role) {
        //1.创建PageBean对象
        PageBean<User> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        String state = "正常";
        List<User> as = userMapper.getUserList(userId, username, role,state);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<User> p = (Page<User>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public PageBean<User> getBlackUserList(Integer pageNum, Integer pageSize, String username, String role, String state) {
        //1.创建PageBean对象
        PageBean<User> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        List<User> as = userMapper.getBlackUserList(userId, username, role, state);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<User> p = (Page<User>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void updateState(Integer id,String state) {
        userMapper.updateState(id,state);
    }


}
